package com.liuzhengting.security.interceptor;

import com.alibaba.fastjson.JSON;
import com.liuzhengting.common.model.ErrorCode;
import com.liuzhengting.common.model.GeneralResult;
import com.liuzhengting.security.utils.JsonResult;
import com.liuzhengting.security.utils.json.ResultCode;
import com.liuzhengting.security.utils.json.ResultTool;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class CustomizeAuthenticationEntryPoint implements AuthenticationEntryPoint {
	
	@Override
	public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
//		JsonResult result = ResultTool.fail(ResultCode.USER_NOT_LOGIN);
		GeneralResult generalResult = new GeneralResult();
		generalResult.setErr(ErrorCode.USER_UNAUTHORIZED_USER);
		httpServletResponse.setContentType("text/json;charset=utf-8");
		httpServletResponse.getWriter().write(JSON.toJSONString(generalResult));
	}

}
